package 力扣._00周赛._375周赛.问题2;

import java.util.ArrayList;
import java.util.List;

class Solution {
    public static long powerMod(long a, long b, long m) {
        long result = 1;
        a = a % m; // 取模是为了防止 a*b 的结果超过 long 类型的范围

        while (b > 0) {
            // 如果 b 是奇数，将结果乘以当前的 a
            if (b % 2 == 1) {
                result = (result * a) % m;
            }

            // 将 a 求平方，b 右移一位
            a = (a * a) % m;
            b /= 2;
        }

        return result;
    }

    public List<Integer> getGoodIndices(int[][] variables, int target) {
        List<Integer> indexs = new ArrayList<>();
        for (int i = 0; i < variables.length; i++) {
            int[] variable = variables[i];
            long t = powerMod(variable[0], variable[1], 10);
            t = powerMod(t, variable[2], variable[3]);
            if (target == t)
                indexs.add(i);
        }
        return indexs;

    }
}